package com.eobjectsoft.etranslator.preferences;

import com.eobjectsoft.etranslator.*;
import com.eobjectsoft.etranslator.engine.TranslateEngine;
import java.util.List;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class TranslatePreferencePage extends PreferencePage
    implements IWorkbenchPreferencePage
{
	
	private IPreferenceStore mStore;
    Combo cmbFrom;
    Button btnTargets[];
    String supportedLangs[];
    Text txtInterval;
    


    public TranslatePreferencePage()
    {
        supportedLangs = TranslateEngine.getInstance().getAllSupportedLan();
        mStore = ETranslatorPlugin.getDefault().getPreferenceStore();
        setPreferenceStore(mStore);
        setTitle(ETranslatorResources.getString("preferencePage.title"));
    }

    protected Control createContents(Composite parent)
    {
        initializeDialogUnits(parent);
        Composite result = new Composite(parent, 0);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        result.setLayout(layout);
        Label pageDesc = new Label(result, 0);
        pageDesc.setText(ETranslatorResources.getString("preferencePage.title"));
        GridData gd = new GridData(768);
        gd.horizontalSpan = 2;
        pageDesc.setLayoutData(gd);
        createFromGroup(result);
        createToGroup(result);
        return result;
    }

    /**
     * panel for source language
     * @param parent
     */
    private void createFromGroup(Composite parent)
    {
        Group fromLanguageGroup = new Group(parent, 0);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        fromLanguageGroup.setLayout(layout);
        GridData gd = new GridData(768);
        gd.horizontalSpan = 2;
        fromLanguageGroup.setLayoutData(gd);
        Label errorLabel = new Label(fromLanguageGroup, 0);
        errorLabel.setText(ETranslatorResources.getString("preferencePage.interval.desc"));
        gd = new GridData(768);
        gd.horizontalSpan = 2;
        errorLabel.setLayoutData(gd);
        Label sleepLabel = new Label(fromLanguageGroup, 0);
        sleepLabel.setText(ETranslatorResources.getString("preferencePage.interval"));
        txtInterval = new Text(fromLanguageGroup, 2048);
        int interval = mStore.getInt("interval");
        txtInterval.setText(Integer.toString(interval));
        txtInterval.setLayoutData(new GridData(768));
        Label fromLabel = new Label(fromLanguageGroup, 0);
        fromLabel.setText(ETranslatorResources.getString("preferencePage.from.lbl"));
        int index = mStore.getInt("trans_from");
        String sourceLabels[] = new String[supportedLangs.length];
        for(int i = 0; i < sourceLabels.length; i++)
            sourceLabels[i] = Charsets.getCharsetLable(supportedLangs[i]);

        cmbFrom = new Combo(fromLanguageGroup, 8);
        cmbFrom.setItems(sourceLabels);
        cmbFrom.select(index);
        cmbFrom.setLayoutData(new GridData(768));
        cmbFrom.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent e)
            {
                refreshToBtns();
            }

        }
);
    }

    /**
     * panel to target panel
     * @param parent
     */
    private void createToGroup(Composite parent)
    {
        Group toLanguageGroup = new Group(parent, 0);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        toLanguageGroup.setLayout(layout);
        GridData gd = new GridData(768);
        gd.horizontalSpan = 2;
        toLanguageGroup.setLayoutData(gd);
        toLanguageGroup.setText(ETranslatorResources.getString("preferencePage.to.lbl"));
        btnTargets = new Button[supportedLangs.length];
        for(int i = 0; i < supportedLangs.length; i++)
        {
            btnTargets[i] = new Button(toLanguageGroup, 32);
            Label toLabel = new Label(toLanguageGroup, 0);
            toLabel.setText(Charsets.getCharsetLable(supportedLangs[i]));
            btnTargets[i].addSelectionListener(new ButtonSelAdapter(i));
        }

        initToBtns();
    }

    private void initToBtns()
    {
        for(int i = 0; i < btnTargets.length; i++)
        {
            String target = supportedLangs[i];
            btnTargets[i].setSelection(mStore.getBoolean("trans_to" + target));
            if(i == cmbFrom.getSelectionIndex())
                btnTargets[i].setEnabled(false);
        }

    }

    /**
     * refresh button according to selection
     */
    private void refreshToBtns()
    {
        int index = cmbFrom.getSelectionIndex();
        String fromLan = supportedLangs[index];
        List<String> targets = TranslateEngine.getInstance().getToLans(fromLan);
        for(int i = 0; i < btnTargets.length; i++)
        {
            String target = supportedLangs[i];
            if(targets.contains(target))
            {
                btnTargets[i].setEnabled(true);
                btnTargets[i].setSelection(true);
            } else
            {
                btnTargets[i].setSelection(false);
                btnTargets[i].setEnabled(false);
            }
        }

    }

    public void init(IWorkbench iworkbench)
    {
    }

    public boolean performOk()
    {
        try
        {
            String txt = txtInterval.getText();
            int interval = Integer.parseInt(txt);
            mStore.setValue("interval", interval);
            mStore.setValue("trans_from", cmbFrom.getSelectionIndex());
            for(int i = 0; i < btnTargets.length; i++)
            {
                String target = supportedLangs[i];
                if(btnTargets[i].getSelection())
                    mStore.setValue("trans_to" + target, true);
                else
                    mStore.setValue("trans_to" + target, false);
            }

        }
        catch(Exception _ex)
        {
            return false;
        }
        return true;
    }

    
    class ButtonSelAdapter extends SelectionAdapter
    {

        public void widgetSelected(SelectionEvent event)
        {
            Button btn = (Button)event.widget;
            if(btn.getSelection())
                mStore.setValue("trans_to" + supportedLangs[btnIndex], true);
            else
                mStore.setValue("trans_to" + supportedLangs[btnIndex], false);
        }

        int btnIndex;

        public ButtonSelAdapter(int index)
        {
            btnIndex = 0;
            btnIndex = index;
        }
    }

}
